package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
   private UserInfoService userInfoService;
    /**
     * 查询用户资料
     */
    @GetMapping
    public ResponseEntity users(Long userID,@RequestHeader("Authorization") String token){
       /* if (userID==null){
            Claims claims = JwtUtils.getClaims(token);
            Integer id = (Integer) claims.get("id");
            userID = Long.valueOf(id);
        }

        UserInfoVo userInfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
        */

        if (userID==null){
            userID= UserHolder.getUserId();
        }
       UserInfoVo userInfo= userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);

    }
    /*
    * 更新用户资料
    * */
   @PutMapping
    public ResponseEntity users(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token){
     /*  //1.校验token
       boolean b = JwtUtils.verifyToken(token);
       if (!b){
           return ResponseEntity.status(401).body(null);
       }
      // 2.向userinfo中设置id
       Claims claims = JwtUtils.getClaims(token);
       Integer id = (Integer) claims.get("id");
       //3/根据id更新
        userInfo.setId(Long.valueOf(id));
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
        *
        */

       userInfo.setId(UserHolder.getUserId());
       userInfoService.update(userInfo);
       return ResponseEntity.ok(null);
    }
//    更新用户头像
    @PostMapping("/header")
    public ResponseEntity header(MultipartFile headPhoto,@RequestHeader("Authorization") String token) throws IOException {

        userInfoService.updateHead(headPhoto,UserHolder.getUserId());
        return ResponseEntity.ok(null);
    }
//    /*修改手机号--发送验证码*/
//    @PostMapping("/phone/sendVerificationCode")
//    public ResponseEntity sendVerificationCode( @RequestBody Map map)  {
//        String phone = (String) map.get("phone");
//        userInfoService.sendVerificationCode(phone);
//        return ResponseEntity.ok(null);
//    }
    /*修改手机号--校验验证码*/
    /*修改手机号-保存*/



}
